//
//  WPReturnSectionHeaderView.m
//  wobingwoyi
//
//  Created by WGP on 2016/12/22.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "WPReturnSectionHeaderView.h"
#import "WPMRModel.h"
#import "WPReturnListModel.h"

@interface WPReturnSectionHeaderView ()

@property(nonatomic,strong)UIView *titleView;
@property(nonatomic,strong)UILabel *titleLabel;
@property(nonatomic,strong)UIView *baseInfoView;
@property(nonatomic,strong)UIView *ZDView;
@property(nonatomic,strong)UILabel *treamentStateBtn;
@property(nonatomic,strong)UIButton *ZXBtn;
@property(nonatomic,strong)UILabel *diseaseLabel;
@property(nonatomic,strong)UILabel *doctorLabel;
@property(nonatomic,strong)UILabel *hospitalLabel;
@property(nonatomic,strong)UILabel *diseaseTextField;
@property(nonatomic,strong)UILabel *doctorTextField;
@property(nonatomic,strong)UILabel *hospitalTextField;
@property(nonatomic,strong)UILabel *timeLabel;

@property(nonatomic,strong)NSArray *timesArr;

@end
@implementation WPReturnSectionHeaderView

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        
        self.contentView.backgroundColor = GlobalBgColor;
        
//        UIButton *tipBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        self.tipBtn = tipBtn;
//        [tipBtn setImage:[UIImage imageNamed:@"mandatory_star"] forState:UIControlStateNormal];
//        [tipBtn setTitle:@" 至少填写一份诊断凭据" forState:UIControlStateNormal];
//        [tipBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
//        tipBtn.titleLabel.font = [UIFont systemFontOfSize:12];
//        [self.contentView addSubview:tipBtn];
//        tipBtn.hidden = YES;
//        
//        [tipBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.centerY.equalTo(self.contentView);
//            make.right.equalTo(self.contentView).offset(-10);
//        }];
        
         [self setupUI];
    }
    return self;
}

-(void)setupUI
{
    [self titleView];
    [self baseInfoView];
    [self setupLabels];
}

-(UIView *)titleView
{
    if (_titleView == nil)
    {
        _titleView = [UIView new];
        _titleView.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:_titleView];
        [_titleView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView);
            make.left.right.equalTo(self.contentView);
            make.height.mas_equalTo(44);
        }];
        
        self.titleLabel = [UILabel new];
        self.titleLabel.font = [UIFont systemFontOfSize:15];
        self.titleLabel.textColor = GLobalFontColor;
        self.titleLabel.text = @"二诊诊断凭据";
        [_titleView addSubview:_titleLabel];
        
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(_titleView);
        }];
        
        UIImageView *topLine = [UIImageView lineImageView];
        UIImageView *bottomLine = [UIImageView lineImageView];
        [_titleView addSubview:topLine];
        [_titleView addSubview:bottomLine];
        
        [topLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(_titleView);
            make.height.mas_equalTo(0.5);
        }];
        
        [bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(_titleView);
            make.height.mas_equalTo(0.5);
        }];
    }
    return _titleView;
}

-(UIView *)baseInfoView
{
    if (_baseInfoView == nil)
    {
        UIImage *ZDImage = [UIImage imageNamed:@"zhenduan"];
        
        _baseInfoView = [UIView new];
        _baseInfoView.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:_baseInfoView];
        [_baseInfoView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.contentView);
            make.top.equalTo(self.titleView.mas_bottom);
            make.height.mas_equalTo(132);
        }];
        
        self.treamentStateBtn = [UILabel new];
        self.treamentStateBtn.text = @"治疗状态";
        self.treamentStateBtn.textColor = RGB_COLOR(208, 208, 213);
        [_baseInfoView addSubview:self.treamentStateBtn];
        self.treamentStateBtn.font = [UIFont systemFontOfSize:14];
        CGSize size = [self.treamentStateBtn.text sizeWithMaxSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) font:14];
        [self.treamentStateBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(size.width+1);
            make.right.equalTo(_baseInfoView).offset(-8);
            make.top.equalTo(_baseInfoView);
            make.height.mas_equalTo(44);
        }];
        
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"treamentState"]];
        [_baseInfoView addSubview:imageView];
        
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.treamentStateBtn);
            make.right.equalTo(self.treamentStateBtn.mas_left).offset(-8);
            make.width.mas_equalTo(15);
            make.height.mas_equalTo(20);
        }];
        
        UIImageView *lineView = [[UIImageView alloc] init];
        lineView.backgroundColor = GLobalLineColor;
        [_baseInfoView addSubview:lineView];
        
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(0.5);
            make.height.mas_equalTo(24);
            make.centerY.equalTo(imageView);
            make.right.equalTo(imageView.mas_left).offset(-10);
        }];
        
        self.ZXBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.ZXBtn setBackgroundImage:[UIImage imageNamed:@"ZX_normal"] forState:UIControlStateNormal];
        [self.ZXBtn setBackgroundImage:[UIImage imageNamed:@"ZX_highlight"] forState:UIControlStateHighlighted];
        [self.ZXBtn setTitle:@"西医" forState:UIControlStateNormal];
        [self.ZXBtn setTitleColor:RGB_SAME(102) forState:UIControlStateNormal];
        self.ZXBtn .titleLabel.font = [UIFont systemFontOfSize:12];
        [_baseInfoView addSubview:self.ZXBtn];
        
        UIImage *image = [UIImage imageNamed:@"ZX_normal"];
        [self.ZXBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.treamentStateBtn);
            make.right.equalTo(lineView).offset(-10);
            make.width.mas_equalTo(image.size.width);
        }];
        
        UIView *ZDView = [UIView new];
        self.ZDView = ZDView;
        ZDView.backgroundColor = GlobalBgColor;
        [self.contentView addSubview:ZDView];
        [ZDView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(_baseInfoView);
            make.top.equalTo(_baseInfoView.mas_bottom);
            make.height.mas_equalTo(ZDImage.size.height+30);
        }];
        UIImageView *ZDImageView = [[UIImageView alloc] initWithImage:ZDImage];
        [ZDView addSubview:ZDImageView];
        [ZDImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_baseInfoView).offset(10);
            make.centerY.equalTo(ZDView);
        }];
        UILabel *ZDLabel = [UILabel new];
        ZDLabel.textColor = GLobalFontColor;
        ZDLabel.font = [UIFont systemFontOfSize:15];
        ZDLabel.text = @"诊断单";
        [ZDView addSubview:ZDLabel];
        [ZDLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(ZDView);
            make.left.equalTo(ZDImageView.mas_right).offset(10);
        }];
        
        UIView *timeView = [UIView new];
        timeView.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:timeView];
        [timeView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self.contentView);
            make.top.equalTo(ZDView.mas_bottom);
        }];
        self.timeLabel = [UILabel new];
        [timeView addSubview:self.timeLabel];
        self.timeLabel.font = [UIFont systemFontOfSize:15];
        self.timeLabel.textColor = GLobalFontColor;
        self.timeLabel.text = @"二诊时间:";
        [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(10);
            make.centerY.equalTo(timeView);
        }];
        
        UIImageView *lineView1 = [UIImageView lineImageView];
        UIImageView *lineView2 = [UIImageView lineImageView];
        UIImageView *lineView3 = [UIImageView lineImageView];
        UIImageView *lineView4 = [UIImageView lineImageView];
        UIImageView *lineView5 = [UIImageView lineImageView];
        [_baseInfoView addSubview:lineView1];
        [_baseInfoView addSubview:lineView2];
        [_baseInfoView addSubview:lineView3];
        [timeView addSubview:lineView4];
        [timeView addSubview:lineView5];
        lineView1.frame = CGRectMake(10, 44, SCREEN_WIDTH-10, 0.5);
        lineView2.frame = CGRectMake(10, 88, SCREEN_WIDTH-10, 0.5);
        lineView3.frame = CGRectMake(0, 131.5, SCREEN_WIDTH, 0.5);
        lineView4.frame = CGRectMake(0, 0, SCREEN_WIDTH, 0.5);
        [lineView5 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self.contentView);
            make.height.mas_equalTo(0.5);
        }];
    }
    return _baseInfoView;
}

-(void)setupLabels
{
    UILabel *firstLabel = [self labelWith:@"疾病名称:" textColor:GLobalFontColor];
    self.diseaseLabel = firstLabel;
    UILabel *secondLabel = [self labelWith:@"诊治医生:" textColor:GLobalFontColor];
    self.doctorLabel = secondLabel;
    UILabel *fourthLabel = [self labelWith:@"治疗医院:" textColor:GLobalFontColor];
    self.hospitalLabel = fourthLabel;
    
    
    [_baseInfoView addSubview:firstLabel];
    [_baseInfoView addSubview:secondLabel];
    [_baseInfoView addSubview:fourthLabel];
    
    CGSize size = [firstLabel.text sizeWithMaxSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) font:15];
    CGFloat margin = (44-size.height)/2.0;
    
    [firstLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_baseInfoView).offset(10);
        make.width.mas_equalTo(size.width+1);
        make.top.equalTo(_baseInfoView).offset(margin);
    }];
    
    [secondLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.width.equalTo(firstLabel);
        make.top.equalTo(firstLabel.mas_bottom).offset(2*margin);
    }];
    
    [fourthLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.width.equalTo(firstLabel);
        make.top.equalTo(secondLabel.mas_bottom).offset(2*margin);
    }];
    
    
    self.diseaseTextField = [self labelWith:@"请输入疾病名称" textColor:RGB_COLOR(208, 208, 213)];
    self.doctorTextField = [self labelWith:@"请输入诊治医生" textColor:RGB_COLOR(208, 208, 213)];
    self.hospitalTextField = [self labelWith:@"请输入治疗医院" textColor:RGB_COLOR(208, 208, 213)];
    [_baseInfoView addSubview:self.diseaseTextField];
    [_baseInfoView addSubview:self.doctorTextField];
    [_baseInfoView addSubview:self.hospitalTextField];
    
    [self.diseaseTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(42);
        make.centerY.equalTo(self.diseaseLabel);
        make.right.equalTo(self.ZXBtn.mas_left).offset(5);
        make.left.equalTo(self.diseaseLabel.mas_right).offset(8);
    }];
    
    [self.doctorTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.height.equalTo(self.diseaseTextField);
        make.right.equalTo(_baseInfoView).offset(-10);
        make.centerY.equalTo(self.doctorLabel);
    }];
    
    [self.hospitalTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.height.equalTo(self.diseaseTextField);
        make.right.equalTo(_baseInfoView).offset(-10);
        make.centerY.equalTo(self.hospitalLabel);
    }];
}

-(void)setSection:(NSInteger)section
{
    _section = section;
    self.titleLabel.text = [NSString stringWithFormat:@"%@诊诊断凭据",self.timesArr[section]];
}

-(void)setModel:(WPMRModel *)model
{
    _model = model;
    
    WPReturnListModel *listModel = self.model.returnVisit_list[self.section-1];
    
    self.diseaseTextField.text = listModel.name;
    self.doctorTextField.text = listModel.doctor;
    self.hospitalTextField.text = listModel.hospital;
    self.treamentStateBtn.text = listModel.cureStatus;
    [self.ZXBtn setTitle:listModel.type forState:UIControlStateNormal];
    self.timeLabel.text = [NSString stringWithFormat:@"%@诊时间:  %@",self.timesArr[self.section],listModel.treatmentTime];
    
    self.diseaseTextField.textColor = GLobalFontColor;
    self.doctorTextField.textColor = GLobalFontColor;
    self.hospitalTextField.textColor = GLobalFontColor;
    self.treamentStateBtn.textColor = GLobalFontColor;
}

-(UILabel *)labelWith:(NSString *)title textColor:(UIColor *)textColor
{
    UILabel *label = [UILabel new];
    label.text = title;
    label.textColor = textColor;
    label.font = [UIFont systemFontOfSize:15];
    
    return label;
}

-(NSArray *)timesArr
{
    if (_timesArr == nil)
    {
        _timesArr = [NSArray arrayWithObjects:@"一",@"二",@"三",@"四",@"五",@"六",@"七",@"八",@"九",@"十",@"十一",@"十二", nil];
    }
    return _timesArr;
}

@end
